package com.zp.manager.disruptor;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Disruptor 配置类
 */
@Configuration
public class DisruptorConfig {

    @Resource
    private PictureEditEventWorkHandler pictureEditEventWorkHandler;

    /**
     * 初始化 Disruptor
     *
     * @return Disruptor
     */
    @Bean("pictureEditEventDisruptor")
    public Disruptor<PictureEditEvent> messageModelRingBuffer() {
        // ringBuffer 环形数组大小
        int ringBufferSize = 1024 * 256;
        // 创建 Disruptor 对象
        Disruptor<PictureEditEvent> disruptor = new Disruptor<>(
                PictureEditEvent::new, // 创建事件工厂
                ringBufferSize, // 创建 RingBuffer
                ThreadFactoryBuilder.create().setNamePrefix("PictureEditEventDisruptor").build() // 创建线程工厂
        );
        // 设置消费者
        disruptor.handleEventsWithWorkerPool(pictureEditEventWorkHandler);
        // 开启 Disruptor
        disruptor.start();
        return disruptor;
    }
}
